package com.excuse.excusebuilder.model;

import com.excuse.excusebuilder.bean.MessageLeft;
import com.excuse.excusebuilder.bean.Post;
import com.excuse.excusebuilder.bean.User;
import com.excuse.excusebuilder.impl.callback.GetLeftMsgsCallback;
import com.excuse.excusebuilder.impl.model.CommunityItemDetailModelInterface;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by U-nookia on 2017/4/20.
 */

public class CommunityItemDetailModel implements CommunityItemDetailModelInterface {

    private List<MessageLeft> msgs;

    public CommunityItemDetailModel() {
        msgs = new ArrayList<>();
    }

    @Override
    public void getLeftMsgsFromBmob(Post post, final GetLeftMsgsCallback callback) {
        BmobQuery<MessageLeft> queryMsgs = new BmobQuery<>();
        queryMsgs.addWhereEqualTo("post",post);
        queryMsgs.include("post,messageTo,messageFrom");
        queryMsgs.order("createdAt");
        queryMsgs.setLimit(1000);
        queryMsgs.findObjects(new FindListener<MessageLeft>() {
            @Override
            public void done(List<MessageLeft> list, BmobException e) {
                if (e!=null){
                    callback.haveError(e.toString());
                    return;
                }
                if (e==null&&list!=null){
                    msgs = list;
                    callback.setList(msgs);
                }
            }
        });
    }

    @Override
    public void queryThumbsUpOnBmob(Post post, final GetLeftMsgsCallback callback) {
        BmobQuery<User> queryUser = new BmobQuery<>();
        queryUser.addWhereRelatedTo("relation", new BmobPointer(post));
        BmobQuery<User> queryUserMine = new BmobQuery<>();
        queryUserMine.addWhereEqualTo("objectId", BmobUser.getCurrentUser().getObjectId());
        List<BmobQuery<User>> queryList = new ArrayList<>();
        queryList.add(queryUser);
        queryList.add(queryUserMine);
        BmobQuery<User> query = new BmobQuery<>();
        query.and(queryList);
        query.findObjects(new FindListener<User>() {
            @Override
            public void done(List<User> list, BmobException e) {
                if (e!=null){
                    callback.haveError(e.toString());
                    return;
                }
                if (e == null && list != null) {
                    if (list.size() != 0) callback.setThumbsUpImg();
                    else callback.cancelThumbsUpImg();
                }
            }
        });
    }

    @Override
    public void saveLeftMsgToBmob(Post post, String input, final GetLeftMsgsCallback callback) {
        final MessageLeft msg = new MessageLeft();
        msg.setContent(input);
        msg.setPost(post);
        msg.setContentTo(post.getContent());
        msg.setMessageFrom(BmobUser.getCurrentUser(User.class));
        msg.setMessageTo(post.getUser());
        msg.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e==null){
                    callback.haveSaveMsg(msg);
                }else {
                    callback.haveError(e.toString());
                }
            }
        });
    }
}
